package com.sfh.sfhshopweb.controller;


import com.sfh.sfhshopweb.dto.ResponseResult;
import com.sfh.sfhshopweb.entity.ToolsFile;
import com.sfh.sfhshopweb.service.IToolsFileService;
import com.sfh.sfhshopweb.utils.CosUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 文件 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-12
 */
@RestController
@RequestMapping("/tools-file")
@Api(tags = "文件控制器")
public class ToolsFileController {

    @Autowired
    private IToolsFileService toolsFileService;

    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    public ResponseResult<ToolsFile> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("modelCode") String modelCode, @RequestParam("busiKey") String busiKey) {
        ToolsFile toolsFile = toolsFileService.uploadFile(file, modelCode, busiKey);
        return ResponseResult.success(toolsFile);
    }

    @PostMapping("/query")
    @ApiOperation(value = "查询文件")
    public ResponseResult<List<ToolsFile>> queryFile(@RequestParam("modelCode") String modelCode, @RequestParam("busiKey") String busiKey) {
        List<ToolsFile> toolsFile = toolsFileService.queryFile(modelCode, busiKey);
        return ResponseResult.success(toolsFile);
    }

    @GetMapping("/getById")
    @ApiOperation(value = "根据id获取文件")
    public ResponseResult<ToolsFile> getFileById(@RequestParam("id") String id) {
        ToolsFile toolsFile = toolsFileService.getById(id);
        toolsFile.setUrl(CosUtils.getEncodeUrl(toolsFile.getFilePath()).toString());
        return ResponseResult.success(toolsFile);
    }

    @PostMapping("/removeById")
    @ApiOperation(value = "删除文件")
    public ResponseResult<Boolean> removeFileById(@RequestParam("id") String id) {
        boolean b = toolsFileService.removeFile(id);
        return ResponseResult.success(b);
    }

}
